package com.app.business.app;

import com.app.business.model.*;
import com.app.business.service.*;
import com.app.system.jpa.PageBean;
import com.app.system.permission.model.DataDict;
import com.app.system.permission.service.DataDictService;
import com.app.system.utils.ParamUtils;
import com.app.system.utils.dataType.BigDecimalUtils;
import com.app.system.utils.exception.Precondition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by ddz on 2019/7/29.
 */

@Controller
@RequestMapping("/app/appStatistics/*")
@SuppressWarnings("all")
public class AppStatisticsController {

    @Autowired
    private StatisticsService statisticsService;
    @Autowired
    private StatisticsForYearService statisticsForYearService;
    @Autowired
    private AzProjectService azProjectService;
    @Autowired
    private ZsProjectService zsProjectService;
    @Autowired
    private DataDictService dataDictService;
    @Autowired
    private BuyHousePersonService buyHousePersonService;


    /**
     * 选房情况汇总表
     * 每个安置项目具体情况
     */
    @RequestMapping("loadChooseHouseZsForAllStatistics")
    public void loadChooseHouseZsForAllStatistics(HttpServletRequest request, ModelMap modelMap) {
        //安置项目集合
        String town = ParamUtils.getString(request, "town", "");
        List<ZsProject> zsProjectList = null;
        if (town != null && town != "") {
            zsProjectList = this.zsProjectService.getZsProjectListByTown(town);
        } else {
            zsProjectList = this.zsProjectService.getZsProjectListForAll();
        }
        modelMap.addAttribute("zsProjectList", zsProjectList);
        Map<String, Map> contractMap = this.statisticsService.getContractZsStatisticsForAll();
        modelMap.addAttribute("contractMap", contractMap);
        List<AzProject> azProjectList = this.azProjectService.azProjectByTownAndChoosedList(town);
        modelMap.addAttribute("azProjectList", azProjectList);

        Map<String, Map<String, Map>> houseCountMap = this.statisticsService.getHouseZsStatisticsByZsProjectIdAndAzProjectId();
        modelMap.addAttribute("houseCountMap", houseCountMap);

    }


    /**
     * 选房情况汇总表
     */
    @RequestMapping("loadChooseHouseAzStatistics")
    public void loadChooseHouseAzStatistics(HttpServletRequest request, ModelMap modelMap) {
        //安置项目集合
        String town = ParamUtils.getString(request, "town", "");
        List<AzProject> azProjectList = null;
        if (town != null && town != "") {
            azProjectList = this.azProjectService.azProjectByTownList(town);
        } else {
            azProjectList = this.azProjectService.azProjectList();
        }
        Map<String, Map> contractMap = this.statisticsService.getContractStatisticsByAzProjectId();
        Map<String, Map> houseMap = this.statisticsService.getHouseStatisticsByAzProjectId();
        Map<String, Map> storageRoomMap = this.statisticsService.getStorageRoomCountMapByAzProjectId();
        Map<String, Map> shopHouseMap = this.statisticsService.getShopHouseCountMapByAzProjectId();
        Map<String, Map> carLotMap = this.statisticsService.getCarLotCountMapByAzProjectId();
        Map<String, Integer> countMap = this.statisticsService.getZsProjectCountByAzProject();


        modelMap.addAttribute("azProjectList", azProjectList);
        modelMap.addAttribute("contractMap", contractMap);
        modelMap.addAttribute("houseMap", houseMap);
        modelMap.addAttribute("storageRoomMap", storageRoomMap);
        modelMap.addAttribute("shopHouseMap", shopHouseMap);
        modelMap.addAttribute("carLotMap", carLotMap);
        modelMap.addAttribute("countMap", countMap);
        modelMap.addAttribute("bigUtils", new BigDecimalUtils());

    }

    /**
     * 选房情况汇总表
     * 每个安置项目具体情况
     */
    @RequestMapping("loadChooseHouseZsStatistics")
    public void loadChooseHouseZsStatistics(HttpServletRequest request, ModelMap modelMap) {
        Integer azProjectId = ParamUtils.getInt(request, "azProjectId", 0);
        AzProject azProject = this.azProjectService.findOne(azProjectId);
        Precondition.checkSqdsArguement(azProject != null, "安置项目不存在！");
        String zsProjectIdStr = this.statisticsService.findZsProjectIdSetByAzProjectId(azProject.getId());
        List<ZsProject> zsProjectList = this.zsProjectService.getZsProjectListByZsProjectIdStr(zsProjectIdStr);

        Map<String, Map> contractMap = this.statisticsService.getContractZsStatisticsByAzProjectId(azProject.getId());
        Map<String, Map> houseMap = this.statisticsService.getHouseZsStatisticsByAzProjectId(azProject.getId());
        Map<String, Map> storageRoomMap = this.statisticsService.getStorageRoomZsStatisticsByAzProjectId(azProject.getId());
        Map<String, Map> shopHouseMap = this.statisticsService.getShopHouseZsStatisticsByAzProjectId(azProject.getId());
        Map<String, Map> carLotMap = this.statisticsService.getCarLotZsStatisticsByAzProjectId(azProject.getId());
        modelMap.addAttribute("azProject", azProject);
        modelMap.addAttribute("zsProjectList", zsProjectList);
        modelMap.addAttribute("contractMap", contractMap);
        modelMap.addAttribute("houseMap", houseMap);
        modelMap.addAttribute("storageRoomMap", storageRoomMap);
        modelMap.addAttribute("shopHouseMap", shopHouseMap);
        modelMap.addAttribute("carLotMap", carLotMap);

    }


    /**
     * 选房情况一览表
     */
    @RequestMapping("loadProgressAzStatistics")
    public void loadProgressAzStatistics(HttpServletRequest request, ModelMap modelMap) {
        //安置项目集合
        String town = ParamUtils.getString(request, "town", "");
        List<AzProject> azProjectList = null;
        if (town != null && town != "") {
            azProjectList = this.azProjectService.azProjectByTownList(town);
        } else {
            azProjectList = this.azProjectService.azProjectList();
        }
        Map<String, Map> contractMap = this.statisticsService.getContractProgressStatisticsByAzProjectId();
        Map<String, Map> houseMap = this.statisticsService.getHouseProgressStatisticsByAzProjectId();
        Map<String, Map> carLotMap = this.statisticsService.getCarLotProgressStatisticsByAzProjectId();
        Map<String, Map> storageRoomMap = this.statisticsService.getStorageRoomProgressStatisticsByAzProjectId();

        modelMap.addAttribute("azProjectList", azProjectList);
        modelMap.addAttribute("contractMap", contractMap);
        modelMap.addAttribute("houseMap", houseMap);
        modelMap.addAttribute("carLotMap", carLotMap);
        modelMap.addAttribute("storageRoomMap", storageRoomMap);

    }

    /**
     * 选房情况一览表
     * 安置区内征收项目统计
     */
    @RequestMapping("loadProgressZsStatistics")
    public void loadProgressZsStatistics(HttpServletRequest request, ModelMap modelMap) {
        Integer azProjectId = ParamUtils.getInt(request, "azProjectId", 0);
        AzProject azProject = this.azProjectService.findOne(azProjectId);
        Precondition.checkSqdsArguement(azProject != null, "安置项目不存在！");
        String zsProjectIdStr = this.statisticsService.findZsProjectIdSetByAzProjectId(azProject.getId());
        List<ZsProject> zsProjectList = this.zsProjectService.getZsProjectListByZsProjectIdStr(zsProjectIdStr);

        Map<String, Map> contractMap = this.statisticsService.getContractProgressZsStatisticsByAzProjectId(azProjectId);
        Map<String, Map> houseMap = this.statisticsService.getHouseProgressZsStatisticsByAzProjectId(azProjectId);
        Map<String, Map> carLotMap = this.statisticsService.getCarLotProgressZsStatisticsByAzProjectId(azProjectId);
        Map<String, Map> storageRoomMap = this.statisticsService.getStorageRoomProgressZsStatisticsByAzProjectId(azProjectId);

        modelMap.addAttribute("azProject", azProject);
        modelMap.addAttribute("zsProjectList", zsProjectList);
        modelMap.addAttribute("contractMap", contractMap);
        modelMap.addAttribute("houseMap", houseMap);
        modelMap.addAttribute("carLotMap", carLotMap);
        modelMap.addAttribute("storageRoomMap", storageRoomMap);

    }

    /**
     * 已选房源详细查看界面
     */
    @RequestMapping("loadSelectedHouseShow")
    public void loadSelectedHouseShow(HttpServletRequest request, ModelMap modelMap) {
        Integer azProjectId = ParamUtils.getInt(request, "azProjectId", 0);
        AzProject azProject = this.azProjectService.findOne(azProjectId);

        PageBean<House> housePageBean = this.statisticsService.getStatisticsSelectedHousePageBean(request);
        List<House> houseList = housePageBean.getDatas();
        Map<String, DataDict> houseHoldMap = this.dataDictService.getMapByParentNameForValue("房屋户型");
        Map<String, List<Map>> buyHousePersonHouseMap = this.buyHousePersonService.getBuyHousePersonByHouse();

        modelMap.addAttribute("houseList", houseList);
        modelMap.addAttribute("houseHoldMap", houseHoldMap);
        modelMap.addAttribute("azProject", azProject);
        modelMap.addAttribute("buyHousePersonHouseMap", buyHousePersonHouseMap);
    }

    /**
     * 已选车位详细查看界面
     */
    @RequestMapping("loadSelectedCarLotShow")
    public void loadSelectedCarLotShow(HttpServletRequest request, ModelMap modelMap) {
        Integer azProjectId = ParamUtils.getInt(request, "azProjectId", 0);
        AzProject azProject = this.azProjectService.findOne(azProjectId);

        PageBean<CarLot> carLotPageBean = this.statisticsService.getStatisticsSelectedCarLotPageBean(request);
        List<CarLot> carLotList = carLotPageBean.getDatas();
        Map<String, DataDict> carLotTypeMap = this.dataDictService.getMapByParentNameForValue("车位类型");
        Map<String, List<Map>> buyHousePersonCarLotMap = this.buyHousePersonService.getBuyHousePersonByCarLot();

        modelMap.addAttribute("carLotList", carLotList);
        modelMap.addAttribute("carLotTypeMap", carLotTypeMap);
        modelMap.addAttribute("azProject", azProject);
        modelMap.addAttribute("buyHousePersonCarLotMap", buyHousePersonCarLotMap);
    }

    /**
     * 已选储物间详细查看界面
     */
    @RequestMapping("loadSelectedStorageRoomShow")
    public void loadSelectedStorageRoomShow(HttpServletRequest request, ModelMap modelMap) {
        Integer azProjectId = ParamUtils.getInt(request, "azProjectId", 0);
        AzProject azProject = this.azProjectService.findOne(azProjectId);

        PageBean<StorageRoom> storageRoomPageBean = this.statisticsService.getStatisticsSelectedStorageRoomPageBean(request);
        List<StorageRoom> storageRoomList = storageRoomPageBean.getDatas();
        Map<String, List<Map>> buyHousePersonStorageRoomMap = this.buyHousePersonService.getBuyHousePersonByStorageRoom();

        modelMap.addAttribute("storageRoomList", storageRoomList);
        modelMap.addAttribute("azProject", azProject);
        modelMap.addAttribute("buyHousePersonStorageRoomMap", buyHousePersonStorageRoomMap);
    }

    /**
     * 已选店铺详细查看界面
     */
    @RequestMapping("loadSelectedShopHouseShow")
    public void loadSelectedShopHouseShow(HttpServletRequest request, ModelMap modelMap) {
        Integer azProjectId = ParamUtils.getInt(request, "azProjectId", 0);
        AzProject azProject = this.azProjectService.findOne(azProjectId);

        PageBean<ShopHouse> shopHousePageBean = this.statisticsService.getStatisticsSelectedShopHousePageBean(request);
        List<ShopHouse> shopHouseList = shopHousePageBean.getDatas();
        Map<String, List<Map>> buyHousePersonStorageRoomMap = this.buyHousePersonService.getBuyHousePersonByStorageRoom();

        modelMap.addAttribute("shopHouseList", shopHouseList);
        modelMap.addAttribute("azProject", azProject);
//        modelMap.addAttribute("buyHousePersonStorageRoomMap", buyHousePersonStorageRoomMap);
    }

    /**
     * 剩余房源详细查看界面
     */
    @RequestMapping("houseShow")
    public void houseShow(HttpServletRequest request, ModelMap modelMap) {
        Integer azProjectId = ParamUtils.getInt(request, "azProjectId", 0);
        AzProject azProject = this.azProjectService.findOne(azProjectId);
        String town = ParamUtils.getString(request, "town", "");

        PageBean<House> housePageBean = this.statisticsService.getStatisticsHousePageBean(request);
        List<House> houseList = housePageBean.getDatas();
        Map<String, DataDict> houseHoldMap = this.dataDictService.getMapByParentNameForValue("房屋户型");
        Map<String, DataDict> houseTypeMap = this.dataDictService.getMapByParentNameForValue("房屋类型");

        modelMap.addAttribute("houseList", houseList);
        modelMap.addAttribute("houseHoldMap", houseHoldMap);
        modelMap.addAttribute("houseTypeMap", houseTypeMap);
        modelMap.addAttribute("azProject", azProject);
    }

    /**
     * 剩余店铺详细查看界面
     */
    @RequestMapping("shopHouseShow")
    public void shopHouseShow(HttpServletRequest request, ModelMap modelMap) {
        Integer azProjectId = ParamUtils.getInt(request, "azProjectId", 0);
        AzProject azProject = this.azProjectService.findOne(azProjectId);
        String town = ParamUtils.getString(request, "town", "");

        PageBean<ShopHouse> shopHousePageBean = this.statisticsService.getStatisticsShopHousePageBean(request);
        List<ShopHouse> shopHouseList = shopHousePageBean.getDatas();

        modelMap.addAttribute("shopHouseList", shopHouseList);
        modelMap.addAttribute("azProject", azProject);
    }

    /**
     * 剩余车位详细查看界面
     */
    @RequestMapping("carLotShow")
    public void carLotShow(HttpServletRequest request, ModelMap modelMap) {
        Integer azProjectId = ParamUtils.getInt(request, "azProjectId", 0);
        AzProject azProject = this.azProjectService.findOne(azProjectId);
        String town = ParamUtils.getString(request, "town", "");

        PageBean<CarLot> carLotPageBean = this.statisticsService.getStatisticsCarLotPageBean(request);
        List<CarLot> carLotList = carLotPageBean.getDatas();
        Map<String, DataDict> carLotTypeMap = this.dataDictService.getMapByParentNameForValue("车位类型");

        modelMap.addAttribute("carLotList", carLotList);
        modelMap.addAttribute("carLotTypeMap", carLotTypeMap);
        modelMap.addAttribute("azProject", azProject);
    }

    /**
     * 剩余储物间详细查看界面
     */
    @RequestMapping("storageRoomShow")
    public void storageRoomShow(HttpServletRequest request, ModelMap modelMap) {
        Integer azProjectId = ParamUtils.getInt(request, "azProjectId", 0);
        AzProject azProject = this.azProjectService.findOne(azProjectId);
        String town = ParamUtils.getString(request, "town", "");

        PageBean<StorageRoom> storageRoomPageBean = this.statisticsService.getStatisticsStorageRoomPageBean(request);
        List<StorageRoom> storageRoomList = storageRoomPageBean.getDatas();

        modelMap.addAttribute("storageRoomList", storageRoomList);
        modelMap.addAttribute("azProject", azProject);
    }

    /**
     * 协议详细查看界面
     */
    @RequestMapping("loadContractShow")
    public void loadContractShow(HttpServletRequest request, ModelMap modelMap) {
        Integer azProjectId = ParamUtils.getInt(request, "azProjectId", 0);
        AzProject azProject = this.azProjectService.findOne(azProjectId);

        List<House> houseList = this.statisticsService.getStatisticsSelectedContractList(request);
        Map<String, DataDict> houseHoldMap = this.dataDictService.getMapByParentNameForValue("房屋户型");

        modelMap.addAttribute("houseList", houseList);
        modelMap.addAttribute("houseHoldMap", houseHoldMap);
        modelMap.addAttribute("azProject", azProject);
    }

    /**
     * 选房情况汇总表
     */
    @RequestMapping("loadHouseStatistics")
    public void loadHouseStatistics(HttpServletRequest request, ModelMap modelMap) {
        Map<String, String> townMap = this.dataDictService.getMapByParentNameForValue_Name("乡镇");


        //安置项目集合
        String town = ParamUtils.getString(request, "townCode", "");
        List<AzProject> azProjectList = null;
        if (town != null && town != "") {
            azProjectList = this.azProjectService.azProjectByTownList(town);
        } else {
            azProjectList = this.azProjectService.azProjectList();
        }
        Map<String, Map> houseMap = this.statisticsService.getHouseByAzProjectId();
        Map<String, Map> storageRoomMap = this.statisticsService.getStorageRoomCountMapByAzProjectId();
        Map<String, Map> shopHouseMap = this.statisticsService.getShopHouseCountMapByAzProjectId();
        Map<String, Map> carLotMap = this.statisticsService.getCarLotCountMapByAzProjectId();


        modelMap.addAttribute("town", town);
        modelMap.addAttribute("azProjectList", azProjectList);
        modelMap.addAttribute("townMap", townMap);
        modelMap.addAttribute("houseMap", houseMap);
        modelMap.addAttribute("shopHouseMap", shopHouseMap);
        modelMap.addAttribute("storageRoomMap", storageRoomMap);
        modelMap.addAttribute("carLotMap", carLotMap);
        modelMap.addAttribute("bigUtils", new BigDecimalUtils());

    }

    /**
     * 全区汇总根据年份
     * 选房情况汇总表
     */
    @RequestMapping("loadChooseHouseByYear")
    public void loadChooseHouseByYear(HttpServletRequest request, ModelMap modelMap) {
        Map<String, DataDict> townMap = this.dataDictService.getMapByParentNameForValue("乡镇");

        //获取当前账号所在乡镇

        String townCode = ParamUtils.getString(request, "town", "");
        List<String> yearList = this.statisticsForYearService.getChooseHouseYearList(townCode);
        List<String> townList = new ArrayList<>();
        if (townCode == null || townCode.equals("")) {
            townList = this.azProjectService.getTownList();
        } else {
            townList.add(townCode);
        }
        Map<String, Map<String, Map>> resultMap = this.statisticsForYearService.getHouseStaticsForYear(townCode);
        Map<String, Map<String, Map>> storageRoomMap = this.statisticsForYearService.getStorageRoomStaticsForYear(townCode);
        Map<String, Map<String, Map>> shopHouseMap = this.statisticsForYearService.getShopHouseStaticsForYear(townCode);
        Map<String, Map<String, Map>> carLotMap = this.statisticsForYearService.getCarLotStaticsForYear(townCode);

        modelMap.addAttribute("townCode", townCode);
        modelMap.addAttribute("townMap", townMap);
        modelMap.addAttribute("yearList", yearList);
        modelMap.addAttribute("townList", townList);
        modelMap.addAttribute("resultMap", resultMap);
        modelMap.addAttribute("storageRoomMap", storageRoomMap);
        modelMap.addAttribute("shopHouseMap", shopHouseMap);
        modelMap.addAttribute("carLotMap", carLotMap);

    }

    /**
     * 各乡镇汇总根据年份
     * 选房情况汇总表
     */
    @RequestMapping("loadChooseHouseForTownByYear")
    public void loadChooseHouseForTownByYear(HttpServletRequest request, ModelMap modelMap) {
        Map<String, DataDict> townMap = this.dataDictService.getMapByParentNameForValue("乡镇");
        String townCode = ParamUtils.getString(request, "town", "");
        String yearStr = ParamUtils.getString(request, "yearStr", "");

        List<String> yearList = this.statisticsForYearService.getChooseHouseYearList(townCode);
        List<String> townList = null;
        if (townCode != null) {
            townList = this.azProjectService.getTownList(townCode);
        } else {
            townList = this.azProjectService.getTownList();
        }
        Map<String, Map<String, Map>> resultMap = this.statisticsForYearService.getHouseStaticsForYearAndTown(townCode, yearStr);
        Map<String, Map<String, Map>> storageRoomMap = this.statisticsForYearService.getStorageRoomStaticsForYearAndTown(townCode, yearStr);
        Map<String, Map<String, Map>> shopHouseMap = this.statisticsForYearService.getShopHouseStaticsForYearAndTown(townCode, yearStr);
        Map<String, Map<String, Map>> carLotMap = this.statisticsForYearService.getCarLotStaticsForYearAndTown(townCode, yearStr);

        modelMap.addAttribute("townCode", townCode);
        modelMap.addAttribute("yearStr", yearStr);
        modelMap.addAttribute("townMap", townMap);
        modelMap.addAttribute("yearList", yearList);
        modelMap.addAttribute("townList", townList);
        modelMap.addAttribute("resultMap", resultMap);
        modelMap.addAttribute("storageRoomMap", storageRoomMap);
        modelMap.addAttribute("shopHouseMap", shopHouseMap);
        modelMap.addAttribute("carLotMap", carLotMap);

    }

    /**
     * 各乡镇汇总根据年份、片区、安置区
     * 获取征收项目详情
     */
    @RequestMapping("loadZsChooseHouseByYear")
    public void loadZsChooseHouseByYear(HttpServletRequest request, ModelMap modelMap) {
        Map<String, DataDict> townMap = this.dataDictService.getMapByParentNameForValue("乡镇");
        String townCode = ParamUtils.getString(request, "townCode", "");
        String yearStr = ParamUtils.getString(request, "yearStr", "");
        Integer azProjectId = ParamUtils.getInt(request, "azProjectId", 0);

        List<ZsProject> zsProjectList = this.zsProjectService.getZsProjectListByTownCodeAndYearAndAzpId(townCode, yearStr, azProjectId);

        Map<String, Map> resultMap = this.statisticsForYearService.getHouseStaticsForYearAndTownAndAzpIdGroupByZsp(townCode, yearStr, azProjectId);
        Map<String, Map> storageRoomMap = this.statisticsForYearService.getStorageRoomStaticsorYearAndTownAndAzpIdGroupByZsp(townCode, yearStr, azProjectId);
        Map<String, Map> shopHouseMap = this.statisticsForYearService.getShopHouseStaticsorYearAndTownAndAzpIdGroupByZsp(townCode, yearStr, azProjectId);
        Map<String, Map> carLotMap = this.statisticsForYearService.getCarLotStaticsForYearAndTownAndAzpIdGroupByZsp(townCode, yearStr, azProjectId);

        modelMap.addAttribute("townCode", townCode);
        modelMap.addAttribute("yearStr", yearStr);
        modelMap.addAttribute("azProjectId", azProjectId);
        modelMap.addAttribute("zsProjectList", zsProjectList);
        modelMap.addAttribute("townMap", townMap);
        modelMap.addAttribute("resultMap", resultMap);
        modelMap.addAttribute("storageRoomMap", storageRoomMap);
        modelMap.addAttribute("shopHouseMap", shopHouseMap);
        modelMap.addAttribute("carLotMap", carLotMap);

    }


}
